<?php
date_default_timezone_set('Asia/Shanghai');

$script_php_dir = "/soooner/cam_upload_script/php/";

#合并FLV脚本地址
$flv2big = "/soooner/bin/flvrec";

#FLV转MP4脚本地址
$flv2mp4 = "/usr/bin/ffmpeg";

$LOG_DF = date("Ymd");

$request_log = "{$script_php_dir}log/{$LOG_DF}_report_error_file.log";

include_once($script_php_dir."include/mcrypt.php");

##################xml config read#################


function uploadHandle($filepath="",$camid="",$shareid="",$start_time="",$end_time="",$uploadurl){

	global $flv2big,$flv2mp4,$tmp_exe_arr=array();    

	system("$flv2big $filepath/ $start_time $end_time $filepath/",$fstatus);
	
	system("export LD_LIBRARY_PATH=/data/ffmpeg/lib/",$lstatus);
	
	//格式转换
	$exe = exec("$flv2mp4 -i $filepath/$start_time-$end_time.flv -acodec copy -vcodec copy $filepath/$start_time-$end_time.mp4 2>&1 | grep 'lasttimestamp'",$output,$return_var);
	if($exe){
		$tmp_exe_arr = explode(":",$exe);
		$vodtimelen = trim($tmp_exe_arr[1]);
	}		
		
	clearstatcache();
	global $privateKey,$iv;
	$file = $start_time.'-'.$end_time.'.mp4';
	$file_size = filesize($filepath.'/'.$file);
	$input = $camid.'+'.time();
	$token = base64_en(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $input, MCRYPT_MODE_CBC, $iv));
	$f1_upload_url="";
	if(strstr($shareid,'_')){
		#upload file
		$f1_upload_url = $uploadurl[1];
		$data = array(
			"file" =>"@".$filepath."/".$file
			);
		$f1_upload_url .="?utype=road&eventid=$shareid&md5=".md5_file($filepath."/".$file)."&token=$token&size=$file_size&len=$vodtimelen";		
	}else{
		#upload file
		$f1_upload_url = $uploadurl[0];					
		$data = array(
		"camid"=>$camid,
		"shareid"=>$shareid,
		"file_md5"=>md5_file($filepath."/".$file),
		"token"=>$token,
		"file"=>"@".$filepath."/".$file
		);
	}
	echo $f1_upload_url;
	var_dump($data);
	$f1_result = json_decode(uploadByCURL1($data,$f1_upload_url),true);
	var_dump($f1_result);
	
	if($f1_result){
		if($f1_result["result"]==0){
			echo "Uploaded : $filepath/".$file."\n";
			rename($filepath."/".$file,$filepath."/".$file.".old");
		}else if($f1_result["result"]==1){
			if($f1_result["msg"]=='no record effected'){
				echo "Warning : $filepath/".$file."\n";
				rename($filepath."/".$file,$filepath."/".$file.".old");
			}
		}
	}
}

//public-CURL-POST
function uploadByCURL1($post_data,$post_url){
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0 );
	curl_setopt($curl, CURLOPT_TIMEOUT, 600 );
	curl_setopt($curl, CURLOPT_URL, $post_url);
	curl_setopt($curl, CURLOPT_LOW_SPEED_LIMIT, 102400);
	curl_setopt($curl, CURLOPT_POST, 1 );
	curl_setopt($curl, CURLOPT_POSTFIELDS,  $post_data);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($curl, CURLOPT_USERAGENT,"Mozilla/4.0");
	$result = curl_exec($curl);
	$error = curl_error($curl);
	curl_close($curl);
	########Generate log file of upload request
	global $request_log;
	$logRequest = fopen($request_log,"a");
	fwrite($logRequest,date("Y-m-d H:i:s")."$post_url".json_encode($post_data)."$result $error"."\n");
	fclose($logRequest);
	return $error ? $error : $result;
}


